package edu.jsnu.purchase.ticket.controller.common;

import edu.jsnu.service.ticket.user.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import util.dataEncrype.EncryptUtil;
import edu.jsnu.purchase.ticket.beans.SessionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public abstract class AbstractController {

    @ModelAttribute
    public void initPath(HttpServletRequest request, HttpServletResponse response, ModelMap model){
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
        model.addAttribute("base", basePath);

        User userDto = SessionContext.getAuthUser(request);
        if (userDto != null){
            request.getSession().setAttribute("user", userDto);
        }
    }

    protected Long decrypt(String id) {
        return EncryptUtil.decrypt(id);
    }

    protected String encrypt(Long id) {
        return EncryptUtil.encrypt(id);
    }

}
